--Variable Declarations
CAF = {}
CAF.StartingUp = false;
CAF.HasInternet = false;
CAF.InternetEnabled = true //Change this to false if you crash when CAF loads clientside

local DEBUG = true

local Addons = {}
local MainInfoMenuData = nil;
local MainStatusMenuData = nil;
local TopFrame = nil;
local TopText = {};
local TopFrameHasText = false;
local TopLabel = nil
CAF.version = 0.2



local addonlevel = {}
addonlevel["1"] = {}
addonlevel["2"] = {}
addonlevel["3"] = {}
addonlevel["4"] = {}
addonlevel["5"] = {}

local hooks = {}
hooks["think"] = {}
hooks["think2"] = {}
hooks["think3"] = {}
hooks["OnAddonDestruct"] = {}
hooks["OnAddonConstruct"] = {}
hooks["OnAddonExtraOptionChange"] = {}

local function ErrorOffStuff(String)
	Msg( "----------------------------------------------------------------------\n" )
	Msg("-----------Custom Addon Management Framework Error----------\n")
	Msg("----------------------------------------------------------------------\n")
	Msg(tostring(String).."\n")
end

--function Declarations

--Local functions

local function Create_Frames()
	TopFrame = vgui.Create("DFrame");
	TopFrame:SetDraggable( false ) 
	TopFrame:SetTitle("Custom Addon Framework Info")
	TopFrame:SetSize(500, 60)
	TopFrame:SetPos((ScrW( )/2) - 250, 0);
	TopFrame:ShowCloseButton( false )
	TopFrame:SetVisible( false )
	TopLabel = vgui.Create("DLabel", TopFrame)
	TopLabel:SetPos(20, 30);
	TopLabel:SetSize(450, 20)
end

local function Think()
	for k , v in pairs(hooks["think"]) do
		local ok, err = pcall(v)
		if  not ok then
			CAF.WriteToDebugFile("CAF_Hooks", "Think Error " .. err .. "\n")
		end
	end
end
hook.Add("Think", "CAF Think", Think) 

local function Think2()
	for k , v in pairs(hooks["think2"]) do
		local ok, err = pcall(v)
		if  not ok then
			CAF.WriteToDebugFile("CAF_Hooks", "Think2 Error " .. err .. "\n")
		end
	end
end
timer.Create( "CAF Think 2", 0.5, 0, Think2 )


local function Think3()
	for k , v in pairs(hooks["think3"]) do
		local ok, err = pcall(v)
		if  not ok then
			CAF.WriteToDebugFile("CAF_Hooks", "Think3 Error " .. err .. "\n")
		end
	end
end
timer.Create( "CAF Think 3", 1, 0, Think3 )

local function OnAddonDestruct(name)
	if not name then return end
	if(CAF.GetAddonStatus(name)) then
		local ok, err = pcall(Addons[name].__Destruct);
		if not ok then
			CAF.WriteToDebugFile("CAF_Destruct", "Couldn't call destructor for "..name .. " error: " .. err .."\n")
			AddTopInfoMessage("Error unloading Addon: " .. name);
		else
			if err then
				AddTopInfoMessage("Addon: " .. name .. " got disabled");
			else
				AddTopInfoMessage("An error occured when trying to disable Addon: " .. name);
			end
		end
	end
	if not CAF.StartingUp then
		for k , v in pairs(hooks["OnAddonDestruct"]) do
			local ok, err = pcall(v, name)
			if not ok then
				CAF.WriteToDebugFile("CAF_Hooks", "OnAddonDestruct Error " .. err .. "\n")
			end
		end
	end
end

local function OnAddonConstruct(name)
	if not TopFrame then Create_Frames() end
	if not name then return end
	if(!CAF.GetAddonStatus(name)) then
		if Addons[name] then
			local test, err = pcall(Addons[name].__Construct);
			if not test then
				CAF.WriteToDebugFile("CAF_Construct", "Couldn't call constructor for "..name .. " error: " .. err .."\n")
			else
				if err then
				AddTopInfoMessage("Addon: " .. name .. " got enabled");
			else
				AddTopInfoMessage("An error occured when trying to enable Addon: " .. name);
			end
			end
		end
	end
	if not CAF.StartingUp then
		for k , v in pairs(hooks["OnAddonConstruct"]) do
			local ok, err = pcall(v, name)
			if not ok then
				CAF.WriteToDebugFile("CAF_Hooks", "OnAddonConstruct Error " .. err .. "\n")
			end
		end
	end
end

local function OnAddonExtraOptionChange(AddonName, OptionName, NewStatus)
	if not AddonName or not OptionName then return nil, "Missing Argument" end
	for k , v in pairs(hooks["OnAddonExtraOptionChange"]) do
		local ok, err = pcall(v, AddonName, OptionName, NewStatus)
		if not ok then
			CAF.WriteToDebugFile("CAF_Hooks", "OnAddonExtraOptionChange Error " .. err .. "\n")
		end
	end
end

--Global function

function CAF.WriteToDebugFile(filename, message)
	if not filename or not message then return nil , "Missing Argument" end
	if DEBUG then
		ErrorNoHalt("Filename: "..tostring(filename)..", Message: "..tostring(message).."\n")
	end
	local contents = file.Read("CAF_Debug/client/"..filename..".txt") 
	contents = contents or "" 
	contents = contents .. message
	file.Write("CAF_Debug/client/"..filename..".txt", contents)
end

function CAF.ClearDebugFile(filename)
	if not filename then return nil , "Missing Argument" end
	local contents = file.Read("CAF_Debug/client/"..filename..".txt") 
	contents = contents or "" 
	file.Write("CAF_Debug/client/"..filename..".txt", "")
	return content
end

function CAF.GetSavedAddonStatus(Addon)
	--ToDO Implement
	return true
end

--Not used!, done using Hooks serverside
function CAF.Start()
	
end

--[[
	This function can be called to register a Hook (similar to Hook.add)
	Possibly Hooks are:
		think: Just like the default Think Hook
		think2: Updates every 0.5 seconds
		think3: Updates every Second
		OnEntitySpawn: Get's called when an entity spawns (parameters: Entity, Type(string), Player who spawned it)
		OnAddonDestruct: Gets called when an addon gets disabled (Parameters: Name of addon)
		OnAddonConstruct: Get called when an addon gets Enabled (parameters: Name of addon)
		OnAddonExtraOptionChange: Gets called when an Extra option on an addon changes (Parameters: Addonname, CustomStatusname, NewStatus value)
		
]]
function CAF.AddHook(HookName, func)
	if  not HookName then return false , "No HookName given" end
	if not func then return false , "No function given" end
	if not hooks[HookName] then return false , "This hook doesn't exist" end
	table.insert(hooks[HookName], func)
		if(HookName == "think") then
		if(#hooks[HookName] == 1) then
			Msg("Adding CAF Think Hook");
			hook.Add("Think", "CAF Think", Think) 
		end
	elseif(HookName == "think2") then
		if(#hooks[HookName] == 1) then
			Msg("Adding CAF Think 2 Hook");
			timer.Create( "CAF Think 2", 0.5, 0, Think2 )
		end
	elseif(HookName == "think3") then
		if(#hooks[HookName] == 1) then
			Msg("Adding CAF Think 3 Hook");
			timer.Create( "CAF Think 3", 1, 0, Think3 )
		end
	end
	return true
end

--[[
	Remove the functions you added to a certain hook from here
]]
function CAF.RemoveHook(HookName, func)
	if  not HookName then return false , "No HookName given" end
	if not func then return false , "No function given" end
	if not hooks[HookName] then return false , "This hook doesn't exist" end
	for k, v in pairs(hooks[HookName]) do
		if (v == func) then
			table.remove(hooks[HookName], k)
		end
	end
	if(HookName == "think") then
		if(#hooks[HookName] == 0) then
			Msg("Removing CAF Think Hook");
			hook.Remove("Think", "CAF Think")
		end
	elseif(HookName == "think2") then
		if(#hooks[HookName] == 0) then
			Msg("Removing CAF Think 2 Hook");
			timer.Destroy("CAF Think 2")  
		end
	elseif(HookName == "think3") then
		if(#hooks[HookName] == 0) then
			Msg("Removing CAF Think 3 Hook");
			timer.Destroy("CAF Think 3")  
		end
	end
	return true
end

function CAF.GetExtraOptionStatus(AddonName, OptionName)
	if not AddonName then return nil , "No AddonName given" end
	if not OptionName then return nil , "No OptionName given" end
	if( Addons[AddonName]) then
		if( Addons[AddonName].GetExtraOptions) then
			local tmp =  Addons[AddonName].GetExtraOptions()
			if (tmp) then
				if(tmp[OptionName]) then
					return tmp[OptionName].status
				end
				return nil , "This option wasn't found for this Addon"
			end
		end
		return nil , "No Extra options found for this Addon"
	end
	return nil , "Addon Not Found"	
end

function CAF.GetAddonStatus(AddonName)
	if not AddonName then return nil , "No AddonName given" end
	if( Addons[AddonName]) then
		local ok, status = pcall(Addons[AddonName].GetStatus)
		if(ok) then
			return status
		end
	end
	return nil , "No Status Info Found"
end

function CAF.GetAddonCustomStatus(AddonName)
	if not AddonName then return nil , "No AddonName given" end
	if( Addons[AddonName]) then
		local ok, status = pcall(Addons[AddonName].GetCustomStatus)
		if(ok) then
			return status
		end
	end
	return nil , "No Custom Status Info Found"
end

function CAF.GetAddonVersion(AddonName)
	if not AddonName then return nil , "No AddonName given" end
	if( Addons[AddonName]) then
		local ok, version, version2 = pcall(Addons[AddonName].GetVersion)
		if(ok) then
			return version, version2
		end
	end
	return nil , nil, "No Version Info Found"
end

function CAF.GetAddon(AddonName)
	if not AddonName then return nil , "No AddonName given" end
	return Addons[AddonName]
end

function CAF.RegisterAddon(AddonName, AddonClass, level)
	if not AddonName then return nil , "No AddonName given" end
	if not AddonClass then return nil , "No AddonClass given" end
	if not level then level = "1" end
	Addons[AddonName] = AddonClass
	table.insert(addonlevel[level], AddonName)
	return  true
end

--Server-Client Synchronisation
function CAF.ConstructAddon(Message)
	local name = Message:ReadString()
	OnAddonConstruct(name)
end
usermessage.Hook("CAF_Addon_Construct", CAF.ConstructAddon)

function CAF.DestructAddon(Message)
	local name = Message:ReadString()
	OnAddonDestruct(name)
end
usermessage.Hook("CAF_Addon_Destruct", CAF.DestructAddon)

function CAF.Start(Message)
	CAF.StartingUp = true;
end
usermessage.Hook("CAF_Start_true", CAF.Start)

function CAF.endStart(Message)
	CAF.StartingUp = false;
end
usermessage.Hook("CAF_Start_false", CAF.endStart)


--Menu's
CAF.HTTP = {
	BUGS = "http://www.snakesvx.net",
	INTERNET = "http://www.snakesvx.net",
	VERSION = "http://www.snakesvx.net/help/CAFVERSION.TXT"
};

CAF.LATEST_VERSION = version;
if(!CAF.HasInternet and CAF.InternetEnabled) then
	http.Get(CAF.HTTP.INTERNET,"",
		function(html,size)
			if(html) then
				MsgN("CAF: Client has Internet. Enabled Online-Help");
				CAF.HasInternet = true;
			end
		end
	);
	
	-- Do we have the latest version of CAF installed?
	http.Get(CAF.HTTP.VERSION,"",
		function(html,size)
			local version = tonumber(html);
			if(version) then
				CAF.LATEST_VERSION = version;
				if(CAF.LATEST_VERSION > CAF.version) then
					local message = "Your version is out of date. Please update to version "..CAF.LATEST_VERSION;
					Msg(message);
					AddTopInfoMessage(message)
				end
			end
		end
	);
end

local function ShowNextTopMessage()
	if not TopFrame then Msg("No TopFrame") return end;
	local message = TopText[1];
	table.remove(TopText, 1);
	--Show in the chat window to!!
	local ply = LocalPlayer();
	if ply and ply.ChatPrint then
		ply:ChatPrint(message.."\n")
	else
		Msg(message.."\n")
	end
	--Show in the top window
	TopFrame:SetVisible( true );
	TopFrameHasText = true
	TopLabel:SetText(message);
	timer.Simple(2, ClearTopTextMessage);
end

function ClearTopTextMessage()
	TopLabel:SetText("");
	TopFrame:SetVisible( false )
	if table.Count( TopText) > 0 then
		ShowNextTopMessage()
	else
		TopFrameHasText = false
	end
end

function AddTopInfoMessage(message)
	table.insert(TopText, message);
	if not TopFrameHasText then
		ShowNextTopMessage();
	end
end

local function GetHelpPanel(frame)
	local panel = vgui.Create("DPanel", frame)
	panel:StretchToParent( 6, 80, 6, 6 )
	
	local LeftTree = vgui.Create( "DTree", panel )
	LeftTree:SetSize( 200, panel:GetTall() - 2)
	LeftTree:SetPos(15, 1);

	local RightPanel = vgui.Create("DPanel", panel )
	RightPanel:SetSize(panel:GetWide() - 230, panel:GetTall() - 2);
	RightPanel:SetPos(220, 1);
	
	if not MainInfoMenuData then
		MainInfoMenuData = {}
		if table.Count(Addons) > 0 then
			for k, v in pairs(Addons) do
				local ok, err = pcall(v.GetMenu)
				if ok then
					if err then
						MainInfoMenuData[k] = err
					end
				end
			end
		end
	end
	LeftTree:Clear()
	//Fill the Tree
	if table.Count(MainInfoMenuData) > 0 then
		for k, v in pairs(MainInfoMenuData) do
			//Addon Info
			local title = k;
			local node = LeftTree:AddNode(title)
			//node.Icon:SetImage(devlist.icon)
			for l, w in pairs(v) do
				//Sub Menu's
				local Node = node:AddNode(l)
				function Node.DoClick(btn)
					if(w.hasmenu) then
						//To Implement
					elseif (CAF.HasInternet and w.interneturl) then
						local HTMLTest = vgui.Create("HTML", RightPanel)   
						HTMLTest:StretchToParent( 10, 10, 10, 10 ) 
						HTMLTest:OpenURL(w.interneturl)  
					elseif(w.localurl) then
						local HTMLTest = vgui.Create("HTML", RightPanel)  
						HTMLTest:StretchToParent( 10, 10, 10, 10 ) 
						HTMLTest:SetHTML(file.Read(w.localurl)) 
					end
				end
				function Node.DoRightClick(btn)
					// To Implement
				end
				for m, x in pairs(w) do
					//Links in submenu
					local cnode = Node:AddNode(m)
					function cnode.DoClick(btn)
						if(x.hasmenu) then
							//To Implement
						elseif (CAF.HasInternet and x.interneturl) then
							RightPanel:Clear();
							local HTMLTest = vgui.Create("HTML", RightPanel)  
							HTMLTest:StretchToParent( 10, 10, 10, 10 ) 
							HTMLTest:OpenURL(x.interneturl)  
						elseif(x.localurl) then
							RightPanel:Clear();
							local HTMLTest = vgui.Create("HTML", RightPanel)  
							HTMLTest:StretchToParent( 10, 10, 10, 10 ) 
							HTMLTest:SetHTML(file.Read(x.localurl)) 
						end
					end
					function cnode.DoRightClick(btn)
						// To Implement
					end
				end
			end
		end
	end
	return panel
end

local function GetStatusPanel(frame)
	local panel = vgui.Create("DPanel", frame)
	panel:StretchToParent( 6, 80, 6, 6 )
	--
	--[[local mylist = vgui.Create("DListView", panel)
	mylist:SetMultiSelect(false)
	mylist:SetPos(1,1)
	mylist:SetSize(panel:GetWide()- 2, panel:GetTall()-2)
	local colum =  mylist:AddColumn( "")
	colum:SetFixedWidth( 5 )
	local colum1 =  mylist:AddColumn( "Addon")
	colum1:SetFixedWidth((mylist:GetWide() * 3/10)-5 )
	local colum2 =  mylist:AddColumn( "Option")
	colum2:SetFixedWidth(mylist:GetWide() * 3/10 )
	local colum3 =  mylist:AddColumn( "Version")
	colum3:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum4 =  mylist:AddColumn( " ")
	colum4:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum5 =  mylist:AddColumn( "Status")
	colum5:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum6 =  mylist:AddColumn( " ")
	colum6:SetFixedWidth(mylist:GetWide() * 1/10 )
	mylist.SortByColumn = function()
	
	end
	//Add Special Function to be able to change Lables inside the lines
	//getLineColumn(line, 2):SetTextColor( Color(0,255,0,255) )
	local function getLineColumn(line, id)
		return line.Columns[id]
	end
	-----
	local iteration = 1
	if table.Count(Addons) > 0 then
		for k,v in pairs( Addons ) do
			
			local version, stringversion = "", ""
			if v.GetVersion then
				version, stringversion = v.GetVersion()
			else
				version, stringversion = "0", "UnSpecified"
			end
			local status = ""
			local customstatus = ""
			if v.GetStatus then
				status = v.GetStatus()
			else
				status = nil
			end
			if v.GetCustomStatus then
				customstatus = v.GetCustomStatus()
			end
			local statustxt = ""
			if status and status == true then
				statustxt = "Enabled"
			elseif status != nil and status == false then
				statustxt = "Disabled"
			end
			local line = mylist:AddLine("",  k, "", version, stringversion, statustxt, customstatus)
			if status and status == true then
				getLineColumn(line, 6):SetTextColor( Color(0,255,0,255) )
			elseif status != nil and status == false then
				getLineColumn(line, 6):SetTextColor( Color(255,0,0,255) )
			else
				getLineColumn(line, 6):SetTextColor( Color(255,165,0,255) )
			end
			if v.GetExtraOptions and table.Count(v.GetExtraOptions()) > 0 then
				for key,value in pairs (v.GetExtraOptions()) do
					//Do nothing atm
				end
			end
		end
	end
	]]
	----
	--
	local List = vgui.Create("DPanelList", panel)
	List:EnableHorizontal(false)
	List:EnableVerticalScrollbar(true)
	List:SetPadding(5)
	List:SetSpacing(5)
	--List:SetSize((panel:GetWide() - 10), panel:GetTall())
	List:StretchToParent( 6, 6, 6, 6 )
	List:SetPos(0, 0)
	
	local iteration = 0
	if table.Count(Addons) > 0 then
		for k,v in pairs( Addons ) do
			iteration = iteration + 1;
			local version, stringversion = "", ""
			if v.GetVersion then
				version, stringversion = v.GetVersion()
			else
				version, stringversion = "0", "UnSpecified"
			end
			local status = ""
			local customstatus = ""
			if v.GetStatus then
				status = v.GetStatus()
			else
				status = nil
			end
			if v.GetCustomStatus then
				customstatus = v.GetCustomStatus()
			end
			local statustxt = ""
			if status and status == true then
				statustxt = "Enabled"
			elseif status != nil and status == false then
				statustxt = "Disabled"
			end
			local descriptiontxt = ""
			if v.GetDescription then
				descriptiontxt = v.GetDescription()
			else
				descriptiontxt = {"No Description"};
			end
			
			local cat = vgui.Create("DCollapsibleCategory") 
			cat:SetLabel(k .. " - " ..version)
			local list = vgui.Create("DPanelList",cat)
			local size = 1;
			list:SetPadding(10)
			--local line = mylist:AddLine("",  k, "", version, stringversion, statustxt, customstatus)
			--Add Custom stuff
			local lab = vgui.Create("DLabel", list)
			--Version: 
			lab:SetText("Version: " .. version .. " (" ..stringversion..")")
			lab:SizeToContents()
			list:AddItem(lab)
			size = size + 1;
			--Status
			lab = vgui.Create("DLabel", list)
			lab:SetText("Status: " .. statustxt .. " (" ..customstatus..")")
			lab:SizeToContents()
			list:AddItem(lab)
			
			if status and status == true then
				cat.Header:SetTextColor( Color(0,255,0,255) )
				lab:SetTextColor( Color(0,255,0,255) )
			elseif status != nil and status == false then
				cat.Header:SetTextColor( Color(255,0,0,255) )
				lab:SetTextColor( Color(255,0,0,255) )
			else
				cat.Header:SetTextColor( Color(255,165,0,255) )
				lab:SetTextColor( Color(255,165,0,255) )
			end
			
			size = size + 1;
			
			--Description Blank Line
			lab = vgui.Create("DLabel", list)
			lab:SetText("")
			lab:SizeToContents()
			list:AddItem(lab)
			size = size + 1;
			
			--Description Blank Line
			lab = vgui.Create("DLabel", list)
			lab:SetText("Description:")
			lab:SizeToContents()
			list:AddItem(lab)
			size = size + 1;
			--Description
			for k, v in pairs(descriptiontxt) do
				lab = vgui.Create("DLabel", list)
				lab:SetText(v)
				lab:SizeToContents()
				list:AddItem(lab)
				size = size + 1;
			end
			--End Add Custom Stuff
			list:SetSize(List:GetWide()-10, 15 * size)
			cat:SetContents(list)
			cat:SizeToContents()
			cat:InvalidateLayout()
			if iteration == 1 then
				cat:SetExpanded(true)
			else
				cat:SetExpanded(false)
			end
			List:AddItem(cat)
		end
	end
	
	
	
	return panel
end

function GetClientPanel()
	//TODO Create it
	local panel = vgui.Create("DPanel", frame)
	panel:StretchToParent( 6, 80, 6, 6 )
	return panel;
end

local function GetServerSettingsPanel(frame)
	local panel = vgui.Create("DPanel", frame)
	panel:StretchToParent( 6, 80, 6, 6 )
	--
	local mylist = vgui.Create("DListView", panel)
	mylist:SetMultiSelect(false)
	mylist:SetPos(1,1)
	mylist:SetSize(panel:GetWide()- 2, panel:GetTall()-2)
	local colum =  mylist:AddColumn( "")
	colum:SetFixedWidth( 5 )
	local colum1 =  mylist:AddColumn( "Addon")
	colum1:SetFixedWidth((mylist:GetWide() * 3/10)-5 )
	local colum2 =  mylist:AddColumn( "Option")
	colum2:SetFixedWidth(mylist:GetWide() * 3/10 )
	local colum3 =  mylist:AddColumn( "Version")
	colum3:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum4 =  mylist:AddColumn( " ")
	colum4:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum5 =  mylist:AddColumn( "Status")
	colum5:SetFixedWidth(mylist:GetWide() * 1/10 )
	local colum6 =  mylist:AddColumn( " ")
	colum6:SetFixedWidth(mylist:GetWide() * 1/10 )
	mylist.SortByColumn = function()
	
	end
	//Add Special Function to be able to change Lables inside the lines
	//getLineColumn(line, 2):SetTextColor( Color(0,255,0,255) )
	local function getLineColumn(line, id)
		return line.Columns[id]
	end
	local function SetLineColumn(line, id, element)
		line.Columns[id] = element
	end
	-----
	local iteration = 1
	if table.Count(Addons) > 0 then
		for k,v in pairs( Addons ) do
			
			local version, stringversion = "", ""
			if v.GetVersion then
				version, stringversion = v.GetVersion()
			else
				version, stringversion = "0", "UnSpecified"
			end
			local status = ""
			local customstatus = ""
			if v.GetStatus then
				status = v.GetStatus()
			else
				status = nil
			end
			if v.GetCustomStatus then
				customstatus = v.GetCustomStatus()
			end
			local line = mylist:AddLine("",  k, "", version, stringversion, "", customstatus)
			if status and status == true then
				SetLineColumn(line, 6, vgui.Create("DButton", line))
				local button = getLineColumn(line, 6)
				button:SetText("Disable")
				function button:DoClick()
					RunConsoleCommand("CAF_Addon_Destruct", k)
				end
				button:SetTextColor( Color(0,255,0,255) )
			elseif status != nil and status == false then
				SetLineColumn(line, 6, vgui.Create("DButton", line))
				local button = getLineColumn(line, 6)
				button:SetText("Enable")
				function button:DoClick()
					RunConsoleCommand("CAF_Addon_Construct", k)
				end
				button:SetTextColor( Color(255,0,0,255) )
			else
				getLineColumn(line, 6):SetText("UnKnown")
				getLineColumn(line, 6):SetTextColor( Color(255,165,0,255) )
			end
			if v.GetExtraOptions and table.Count(v.GetExtraOptions()) > 0 then
				for key,value in pairs (v.GetExtraOptions()) do
					//Do nothing atm
				end
			end
		end
	end
	----
	--
	return panel
end

local function GetAboutPanel(frame)
	local panel = vgui.Create("DPanel", frame)
	panel:StretchToParent( 6, 80, 6, 6 )
	--
	local mylist = vgui.Create("DListView", panel)
	mylist:SetMultiSelect(false)
	mylist:SetPos(1,1)
	mylist:SetSize(panel:GetWide()- 2, panel:GetTall()-2)
	local colum =  mylist:AddColumn( "")
	colum:SetFixedWidth(5)
	local colum1 =  mylist:AddColumn( "About")
	colum1:SetFixedWidth(mylist:GetWide() - 5)
	mylist.SortByColumn = function()
	end
	----------
	--Text--
	----------
	mylist:AddLine( "", "Custom Addon Framework" )
	mylist:AddLine( "", "More info to be added" )
	mylist:AddLine( "", "" )
	mylist:AddLine( "", "Made By SnakeSVx" )
	mylist:AddLine( "", "Official website: http://www.snakesvx.net" )
	--
	return panel
end


local function ShowMainInfoMenu()
	local MainFrame= vgui.Create("DFrame")
	//MainFrame:SetDeleteOnClose() 
	MainFrame:SetDraggable( false ) 
	MainFrame:SetTitle("Custom Addon Framework Main Menu")
	MainFrame:SetSize(ScrW() * 0.8, ScrH() * 0.9)
	MainFrame:Center()
	local ContentPanel = vgui.Create( "DPropertySheet", MainFrame )
	ContentPanel:StretchToParent( 4, 26, 4, 4 )
	ContentPanel:AddSheet( "Installed Addons", GetStatusPanel(MainFrame), "gui/silkicons/application", true, true )
	ContentPanel:AddSheet( "Info and Help", GetHelpPanel(MainFrame), "gui/silkicons/box", true, true )
	if LocalPlayer():IsAdmin() then
		ContentPanel:AddSheet( "Server Settings", GetServerSettingsPanel(MainFrame), "gui/silkicons/wrench", true, true )
	end
	ContentPanel:AddSheet( "Client Settings", GetClientPanel(MainFrame), "gui/silkicons/wrench", true, true )
	ContentPanel:AddSheet( "About", GetAboutPanel(MainFrame), "gui/silkicons/group", true, true )
	MainFrame:MakePopup()
end
concommand.Add("Main_CAF_Menu", ShowMainInfoMenu)

--Panel
local function BuildMenu( Panel )
	Panel:ClearControls( )
	Panel:AddHeader( )
	Panel:AddControl( "Button", {
		Label = "Open Menu",
		Text = "Custom Addon Framework",
		Command = "Main_CAF_Menu"
		} )
end

local function CreateMenu( )
	spawnmenu.AddToolMenuOption( "Custom Addon Framework", "Custom Addon Framework", "MainInfoMenu", "Main Info Menu", "", "", BuildMenu, { } )
end
hook.Add( "PopulateToolMenu", "Caf_OpenMenu_AddMenu", CreateMenu )

--Include clientside files

--Core

local Files = file.FindInLua( "caf_core/client/*.lua" )
for k, File in ipairs(Files) do
	Msg("Loading: "..File.."...")
	local ErrorCheck, PCallError = pcall(include, "caf_core/client/"..File)
	if(!ErrorCheck) then
		ErrorOffStuff(PCallError)
	else
		Msg("Loaded: Successfully\n")
	end
end

--Addons
local Files = file.FindInLua( "caf_addons/client/*.lua" )
for k, File in ipairs(Files) do
	Msg("Loading: "..File.."...")
	local ErrorCheck, PCallError = pcall(include, "caf_addons/client/"..File)
	if(!ErrorCheck) then
		ErrorOffStuff(PCallError)
	else
		Msg("Loaded: Successfully\n")
	end
end

/*local Files = file.FindInLua( "caf_addons/shared/*.lua" )
for k, File in ipairs(Files) do
	Msg("Loading: "..File.."...")
	local ErrorCheck, PCallError = pcall(include, "caf_addons/shared/"..File)
	if(!ErrorCheck) then
		ErrorOffStuff(PCallError)
	else
		Msg("Loaded: Successfully\n")
	end
end*/
